package annotations;//: annotations/UseCaseTracker.java
import annotations.UseCase;

import java.lang.reflect.*;
import java.util.*;

/**
 * 注解处理器
 * 通过反射解析注解信息
 * 正式注解的可解析才体现出他的编译时检查以及配置信息的优势
 */
public class UseCaseTracker {
  /**
   *
   * @param useCases 用例代号(id)集合
   * @param cl 使用注解的待处理类类型 passwordUtil
   */
  public static void trackUseCases(List<Integer> useCases, Class<?> cl) {
    //遍历Method
    for(Method m : cl.getDeclaredMethods()) {
      //拿到UseCase注解实例
      UseCase uc = m.getAnnotation(UseCase.class);
      if(uc != null) {
        //通过UseCase拿到元素信息
        System.out.println("Found Use Case:" + uc.id() + " " + uc.description());
        useCases.remove(new Integer(uc.id()));
      }
    }
    for(int i : useCases) {
      System.out.println("Warning: Missing use case-" + i);
    }
  }
  public static void main(String[] args) {
    List<Integer> useCases = new ArrayList<Integer>();
    Collections.addAll(useCases, 47, 48, 49, 50);
    trackUseCases(useCases, PasswordUtils.class);
  }
} /* Output:
Found Use Case:47 Passwords must contain at least one numeric
Found Use Case:48 no description
Found Use Case:49 New passwords can't equal previously used ones
Warning: Missing use case-50
*///:~
